package com.jzg.jcptadmin.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.app.ActivityHelp;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.view.ExtendedViewPager;
import com.jzg.jcptadmin.view.TouchImageView;
import com.jzg.template.refreshrecycleview.RecycleViewBaseAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
*
* -本地图片查看
 * PhotoActivity
* @author sunbl
* created at 2016/1/21 16:09
*/
public class GalleryActivity extends BaseActivity {

    @Bind(R.id.extendedVP)
    ExtendedViewPager extendedVP;
    @Bind(R.id.title)
    TextView title;
    private LinearLayoutManager linearLayoutManager;

    private List<String> imageUrls;
    private int position = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);
        imageUrls = getIntent().getStringArrayListExtra(ActivityHelp.KEY_IMG_URLS);
        position = getIntent().getIntExtra( ActivityHelp.KEY_POSITION,0);
        extendedVP.setAdapter(new GalleryAdapter(imageUrls));
        extendedVP.setCurrentItem(position);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        extendedVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                setShoePerNum(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        setShoePerNum(position);
    }
    private void setShoePerNum(int num) {
        num++;
        if (num <= 0)
            num = 1;
        if (imageUrls != null)
            title.setText(num + "/" + imageUrls.size());
    }

    class PhotoAdapter extends RecycleViewBaseAdapter {
        private Context context;
        private List<String>  list;
        private int index;
        public PhotoAdapter(Context context, List<String> list,int index) {
            this.context = context;
            this.list = list;
            this.index = index;
        }
        public void update(int index){
            this.index = index;
            this.notifyDataSetChanged();
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_photo, null));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            String url = list.get(position);
            if(url.startsWith("http")){
                ImageLoader.getInstance().displayImage(url,myViewHolder.pic);
            }else {
                ImageLoader.getInstance().displayImage("file://" + url, myViewHolder.pic);
            }
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            ImageView pic;
            RelativeLayout picRelative;

            public MyViewHolder(View view) {
                super(view);
                pic = (ImageView) view.findViewById(R.id.pic_item);
                picRelative = (RelativeLayout) view.findViewById(R.id.pic_relative);

            }
        }
    }
    class GalleryAdapter extends PagerAdapter {
        private List<String>  imageList;
        public GalleryAdapter(List<String> list) {
            this.imageList =list;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(GalleryActivity.this);
            img.setScaleType(ImageView.ScaleType.FIT_CENTER);
            String url = imageList.get(position);
            if(url.startsWith("http")){
                ImageLoader.getInstance().displayImage(url,img,appContext.options);
            }else {
                ImageLoader.getInstance().displayImage("file://" + url, img, appContext.options);
            }
            container.addView(img, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
